//
//  GTWeChatDynamicContentView.m
//  GTW
//
//  Created by Ran on 15/11/12.
//  Copyright (c) 2015年 xcode. All rights reserved.
//

#import "GTWeChatDynamicContentView.h"
#import "GTWeChatDynamicTool.h"
#import "GTWeChatDynamicSettings.h"
#import "RanTestModel.h"
#import "GTWeChatDynamicIconImgView.h"
#import "GTWeChatDynamicLinkButton.h"
#import "GTWeChatDynamicTuple.h"

static NSString *const titleForNormalState = @"展开";
static NSString *const titleForSelectedState = @"收起";

#pragma mark - ------------------------Model------------------------

@interface GTWeChatDynamicContentViewModel: NSObject

@property(nonatomic, strong)RanTestModel *sourceModel;
@property(nonatomic, assign)CGFloat theWidth;

@property(nonatomic, assign)CGRect contentLabelFrame;
@property(nonatomic, assign)CGRect unfoldButtonFrame;

@property(nonatomic, assign)BOOL unfold;
@property(nonatomic, assign)BOOL showUnfoldButton;

@end

@implementation GTWeChatDynamicContentViewModel

@end

#pragma mark - ------------------------View------------------------

@interface GTWeChatDynamicContentView()

@property(nonatomic, strong)UILabel *contentLabel;
@property(nonatomic, strong)GTWeChatDynamicLinkButton *unfoldButton;

@property(nonatomic, strong)GTWeChatDynamicContentViewModel *model;

@end

@implementation GTWeChatDynamicContentView

const CGFloat spacingBetweenContentAndFoldButton = 10;

#pragma mark - System

#pragma mark - Private

static CGFloat maxHeight = -1;
+ (CGFloat)maxHeightForFoldState
{
    if (maxHeight < 0) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            maxHeight = [GTWeChatDynamicTool getTextSizeWithText:@"斗fg\n斗fg\n斗fg" attribute:[GTWeChatDynamicSettings contentTextAttributes]].height;
        });
    }
    return maxHeight;
}

#pragma mark - Protocol

+ (GTWeChatDynamicTuple *)getProcessedDataFromSourceData: (id)sourceData withWidth: (CGFloat)theWidth
{
    GTWeChatDynamicContentViewModel *model;
    if ([sourceData isKindOfClass:[GTWeChatDynamicContentViewModel class]]) {
        model = (GTWeChatDynamicContentViewModel *)sourceData;
    }else{
        model = [[GTWeChatDynamicContentViewModel alloc] init];
        model.sourceModel = (RanTestModel *)sourceData;
        model.theWidth = theWidth;
    }
    
    CGFloat viewHeight;
    CGFloat contentX = 0;
    CGFloat contentY = 0;
    CGFloat contentWidth = theWidth;
    CGFloat contentHeight;
    
    CGFloat unfoldBtnX = 0;
    CGFloat unfoldBtnY;
    CGSize unfoldBtnSize = [GTWeChatDynamicTool getTextSizeWithText:titleForNormalState attribute:[GTWeChatDynamicSettings unfoldButtonTextAttributes]];
    CGFloat unfoldBtnWidth = unfoldBtnSize.width;
    CGFloat unfoldBtnHeight = unfoldBtnSize.height;

    CGFloat textHeight = [GTWeChatDynamicTool getTextSizeWithText:model.sourceModel.content width: theWidth attribute:[GTWeChatDynamicSettings contentTextAttributes]].height;
    CGFloat maxContentHeight = [self maxHeightForFoldState];
    CGFloat spacingBetweenContentAndUnfoldBtn = 10;
    
    if (textHeight <= maxContentHeight) {
        contentHeight = textHeight;
        model.contentLabelFrame = CGRectMake(contentX, contentY, contentWidth, contentHeight);
        unfoldBtnY = CGRectGetMaxY(model.contentLabelFrame);
        unfoldBtnHeight = 0;
        model.showUnfoldButton = NO;
    }else if(!model.unfold){
        contentHeight = maxContentHeight;
        model.contentLabelFrame = CGRectMake(contentX, contentY, contentWidth, contentHeight);
        unfoldBtnY = CGRectGetMaxY(model.contentLabelFrame) + spacingBetweenContentAndUnfoldBtn;
        model.showUnfoldButton = YES;
        
    }else{
        contentHeight = textHeight;
        model.contentLabelFrame = CGRectMake(contentX, contentY, contentWidth, contentHeight);
        unfoldBtnY = CGRectGetMaxY(model.contentLabelFrame) + spacingBetweenContentAndUnfoldBtn;
        model.showUnfoldButton = YES;
    }
    
    model.unfoldButtonFrame = CGRectMake(unfoldBtnX, unfoldBtnY, unfoldBtnWidth, unfoldBtnHeight);
    viewHeight = CGRectGetMaxY(model.unfoldButtonFrame);
    
    GTWeChatDynamicTuple *tuple = [[GTWeChatDynamicTuple alloc] init];
    tuple.data = model;
    tuple.height = viewHeight;
    return tuple;
    
}

- (void)feedbackStoreData:(id)data
{
    GTWeChatDynamicContentViewModel *model = (GTWeChatDynamicContentViewModel *)data;
    self.model = model;
    self.contentLabel.frame = model.contentLabelFrame;
    self.unfoldButton.frame = model.unfoldButtonFrame;
    self.unfoldButton.selected = model.unfold;
    self.unfoldButton.hidden = !model.showUnfoldButton;
    self.contentLabel.text = model.sourceModel.content;
}

#pragma mark - Event

- (void)unfoldButtonClicked: (GTWeChatDynamicLinkButton *)button
{
    button.selected = !button.isSelected;
    self.model.unfold = button.selected;
    if ([self.delegate respondsToSelector:@selector(contentViewHeightChanged:)]) {
        [self.delegate contentViewHeightChanged:self];
    }
}

#pragma mark - Getter

- (UILabel *)contentLabel
{
    if (!_contentLabel) {
        self.contentLabel = ({
            UILabel *contentLabel = [GTWeChatDynamicTool labelWithAttribute:[GTWeChatDynamicSettings contentTextAttributes]];
            contentLabel.numberOfLines = 0;
            contentLabel.clipsToBounds = YES;
            contentLabel.lineBreakMode = NSLineBreakByCharWrapping;
            [self addSubview:contentLabel];
            contentLabel;
        });
    }
    return _contentLabel;
}

- (GTWeChatDynamicLinkButton *)unfoldButton
{
    if (!_unfoldButton) {
        self.unfoldButton = ({
            GTWeChatDynamicLinkButton *unfoldButton = [GTWeChatDynamicTool linkButtonWithAttribute:[GTWeChatDynamicSettings unfoldButtonTextAttributes]];
            [unfoldButton setTitle:titleForNormalState forState:UIControlStateNormal];
            [unfoldButton setTitle:titleForSelectedState forState:UIControlStateSelected];
            unfoldButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            [unfoldButton addTarget:self action:@selector(unfoldButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:unfoldButton];
            unfoldButton;
        });
    }
    return _unfoldButton;
}

@end








